package se.webbzon.boltzmann.game.object.inventory.page;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.ImageObserver;

import se.webbzon.boltzmann.game.object.inventory.GameInventory;
import se.webbzon.boltzmann.game.object.inventory.layout.ItemInventoryLayout;
import se.webbzon.boltzmann.input.Button;
import se.webbzon.boltzmann.inventory.Inventory;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.player.Player;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public class ItemInventoryPage extends AbstractGameInventoryPage {

	// A direction enumeration type
	private static enum Direction {LEFT, RIGHT, UP, DOWN};
	
	// The layout of this page
	private final ItemInventoryLayout layout;
	
	// The selected row and column
	private int row, column;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new item inventory page. **/
	public ItemInventoryPage(	GameInventory inventory, 
								Player player, 
								ItemInventoryLayout layout) {
		super(inventory,player);
		this.layout = layout;
		row = 0;
		column = 0;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void onDraw(WorldCameraPane hud) {
		// Make the subsequent drawings transparent
		hud.setTransparency(0.95f);
		
		// Retrieve the images
		final ImageObserver o = hud.getImageObserver();
		final int rows = getPlayer().getInventory().getRows();
		final Image inventory = layout.getInventoryImageSet().get(rows-1);
		final Image info = layout.getInfoImage();
		
		// Retrieve the offsets
		final int 	invXOffset = layout.getInventoryXOffset(),
					invYOffset = layout.getInventoryYOffset(),
					invWidth = layout.getInventoryImageSet().get(0).getWidth(o);
		
		// Draw the backgrounds
		hud.drawImage(inventory, invXOffset, invYOffset);
		hud.drawImage(	info, 
						invXOffset + invWidth + layout.getInfoXSpacing(), 
						invYOffset);
		
		// Retrieve the inventory of the player and the selected item
		final Inventory inv = getPlayer().getInventory();
		final AbstractItem selectedItem = getPlayer().getSelectedItem();
		
		for (int row = 0; row < inv.getRows(); row++) {
			for (int column = 0; column < inv.getColumns(); column++) {
				final AbstractItem item = inv.getItem(row, column);
				if (item != null) {
					if (item == selectedItem) {
						hud.setColor(Color.green);
						hud.fillRect(itemX(column), itemY(row), 32, 32);
					}
					hud.drawImage(	item.getTextureSet().get(0).getImage(),
									itemX(column), 
									itemY(row));
				}
			}
		}
	}
	
	@Override public void onEnterLeft() {
		moveCursor(0,0);
	}
	
	@Override public void onEnterRight() {
		moveCursor(0,5);
	}
	
	@Override public void buttonPressed(Button button) {
		switch (button) {
		case LEFT:
			if (column == 0)
				getInventory().selectLeftArrow();
			else 
				moveCursor(Direction.LEFT);
			break;
		case RIGHT:
			if (column == 5)
				getInventory().selectRightArrow();
			else
				moveCursor(Direction.RIGHT);
			break;
		case UP:
			moveCursor(Direction.UP);
			break;
		case DOWN:
			moveCursor(Direction.DOWN);
			break;
		case ACTION:
			AbstractItem item = getPlayer().getInventory().getItem(row, column);
			if (item != null)
				getPlayer().setSelectedItem(item);
			break;
		case SWITCH:
			final AbstractItem selectedItem = getPlayer().getSelectedItem();
			final Point position = new Point();
			if (getPlayer().getInventory().getPosition(selectedItem,position)) {
				getPlayer().getInventory().swapItems(position.x, position.y, row, column);
			}
			break;
		}
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Moves the cursor to the given row and column in the inventory. **/
	public void moveCursor(int row, int column) {
		final int rows = getPlayer().getInventory().getRows();
		final int columns = getPlayer().getInventory().getColumns();
		if (row >= 0 && row < rows)
			this.row = row;
		if (column >= 0 && column < columns)
			this.column = column;
		getInventory().getCursor().setPosition(itemX(this.column)+16, itemY(this.row)+16);
	}
	
	/** Moves the cursor in the given direction. **/
	public void moveCursor(Direction direction) {
		switch (direction) {
		case UP:
			moveCursor(row-1,column);
			break;
		case DOWN:
			moveCursor(row+1,column);
			break;
		case LEFT:
			moveCursor(row,column-1);
			break;
		case RIGHT:
			moveCursor(row,column+1);
			break;
		}
	}
	
	/** Returns the x-position of an item in a given column. **/
	private int itemX(int column) {
		final int inventoryXOffset = layout.getInventoryXOffset();
		final int itemXOffset = layout.getItemXOffset();
		final int itemXSpacing = layout.getItemXSpacing() + 32;
		return inventoryXOffset + itemXOffset + itemXSpacing*column;
	}
	
	/** Returns the y-position of an item in a given row. **/
	private int itemY(int row) {
		final int inventoryYOffset = layout.getInventoryYOffset();
		final int itemYOffset = layout.getItemYOffset();
		final int itemYSpacing = layout.getItemYSpacing() + 32;
		return inventoryYOffset + itemYOffset + itemYSpacing*row;
	}

}
